home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Macintosh / Tex_Edit_Plus-2.3.3.sit / TE+ 2.3.3 / W.T.H.I. AppleScript? < prev    next >
Text File  |  1998-08-04  |  15KB  |  399 lines

  1.  
  2.                What the Heck is AppleScript?
  3.  
  4. AppleScript is an English-like programming language that allows you to use Apple Events to control applications.
  5.  
  6. Great.  What’s an Apple Event?
  7.  
  8. With the advent of System 7, Apple introduced a powerful new way for applications to communicate with each other.  Each Apple Event represents a single “message” in this digital dialog.
  9.  
  10. For instance, when you double-clicked this document, the Finder actually sent an “open document” Apple Event to Tex-Edit.  Tex-Edit then recognized the event and displayed this document for you.
  11.  
  12. What’s the big deal?
  13.  
  14. Well, Apple Events allow “scriptable” applications to communicate at a very intimate level.  Unlike macros, Apple Events bypass the user interface and are quite efficient.
  15.  
  16. AppleScript puts the power of these Apple Events in the hands of the ordinary user.  It’s just another insanely great advantage that we Mac users enjoy.
  17.  
  18. What does it mean to say an application is “scriptable?”
  19.  
  20. Scriptability implies that the programmer has given AppleScript access to major portions of the application’s inner workings.
  21.  
  22. If AppleScript is so great, why aren’t all applications scriptable?
  23.  
  24. Changing an existing application to make it scriptable involves an effort that I like to refer to as “non-trivial.” Luckily, most significant applications are now scriptable.
  25.  
  26. Do I have to learn to program to use AppleScript?
  27.  
  28. Well, it kind of depends on what you mean by “programming.” The AppleScript dialect is very forgiving and looks a lot like ordinary English.
  29.  
  30. No you don’t understand.  I REALLY hate programming!
  31.  
  32. You’re in luck!
  33.  
  34. Tex-Edit, like many other scriptable applications, is also “recordable.” Apple’s Script Editor can record your actions as you use Tex-Edit.  The resulting “script” (program) can be saved and re-played later.
  35.  
  36. So, AppleScript isn’t just for nerds?
  37.  
  38. The real nerds are the folks who waste time doing repetitive computing chores, instead of letting their Mac do the work.
  39.  
  40. Okay, show me an example of an AppleScript program.
  41.  
  42. Well, let’s see.  How about if we create a “style sheet” program which sets the font of the text to 12 point Monaco (ick!) and then sets the size of the first character of every paragraph to 24 points.
  43.  
  44. Just how much programming work would that require?
  45.  
  46. You just saw it.
  47.  
  48. Huh?
  49.  
  50. Well, actually the program might look something like this:
  51.  
  52.    tell application "Tex-Edit Plus"
  53.       set the font of the text of the first window to "Monaco"
  54.       set the size of the text of the first window to 12
  55.       set the size of the first character of every paragraph of window 1 to 24
  56.    end tell
  57.  
  58. Not bad! What does the first line do?
  59.  
  60. It’s just a shortcut.  Notice how the “tell” and “end tell” lines bracket all the commands.  This saves us a little typing, otherwise the program would read:
  61.  
  62.    set the font of the text of the first window
  63.       of application "Tex-Edit Plus" to "Monaco"
  64.    set the size of the text of the first window
  65.       of application "Tex-Edit Plus" to 12
  66.    set the size of the first character of every paragraph of window 1
  67.       of application "Tex-Edit Plus" to 24
  68.  
  69. But, doesn’t every word have to be in just the right place?
  70.  
  71. AppleScript is a very forgiving programming language.  For example, these two programs do the same thing:
  72.  
  73.    set the size of the first word to ten
  74.    set size of word 1 to 10
  75.  
  76. Okay already! How do I add scripts to Tex-Edit’s  menu?
  77.  
  78. When Script Editor saves a script as a “compiled script,” it creates a script document which can be placed in Tex-Edit’s “Scripts” folder.  When Tex-Edit launches, it adds everything in this folder to its Scripts menu, providing easy access to your favorite scripts.
  79.  
  80. I don’t see a  menu.
  81.  
  82. Either there is no “Scripts” folder in the same folder as Tex-Edit or you have a System version earlier than 7.5.
  83.  
  84. Well, I have System 7.5, but I meticulously removed lots of stuff in the Extensions folder to make my system more stable.
  85.  
  86. Don’t do that!  You probably managed to remove lots of optimized code patches and bug fixes.  It’s not worth the tiny amount of disk space you saved.
  87.  
  88. I want to install AppleScript.  Where can I find it?
  89.  
  90. AppleScript and Script Editor are included free with all Systems since 7.5.  If you have an earlier version of System 7, you can download the latest AppleScript installer from Apple’s web site:
  91. <http://applescript.apple.com/>
  92.  
  93. How can I learn more about writing scripts?
  94.  
  95. AppleScript is a rich, full-featured programming language with variables, subroutines, loops and branches.  If you want to appreciate the full power of AppleScript, I would recommend Danny Goodman’s “AppleScript Handbook.” It’s an excellent source of information, especially for beginners.
  96.  
  97. You should also subscribe to the MACSCRPT mailing list.  It is very active and includes lots of helpful information from lots of AppleScript users.  To subscribe to MACSCRPT:
  98. send email to: <mailto:LISTSERV@dartmouth.edu>
  99. with subject: “subscribe”
  100. and message: “subscribe macscrpt <your full name here>”.
  101.  
  102. Finally, just experiment with Script Editor’s “record” function to see examples of the syntax Tex-Edit uses when sending messages to itself.
  103.  
  104. But, I want to write a script NOW!
  105.  
  106. Okay, first launch Tex-Edit and then launch Script Editor (which should be located in the Apple menu).  To execute an AppleScript command, simply type the command into the Script Editor window, then press the “run” button:
  107.  
  108.      
  109.  
  110. In an AppleScript program, each line is a separate command.  Usually the commands are bracketed by “tell” and “end tell” statements, as explained above.  When the program runs, command lines are executed sequentially from top to bottom.
  111.  
  112. Try typing in and running the following program.  (Lines which start with a double hyphen are optional comments.) 
  113.  
  114. 
  115.  
  116. Once you get a program to do what you want it to do, save it as a “compiled script” document inside of Tex-Edit’s “Scripts” folder.
  117.  
  118. What commands does Tex-Edit understand?
  119.  
  120. Every scriptable application contains a “dictionary” which can be explored using Script Editor.  I have included a brief synopsis of Tex-Edit’s dictionary below, but I would recommend using Script Editor to print out the real thing.
  121.  
  122. How do I interpret what’s in the dictionary?
  123.  
  124. The dictionary is divided up into chapters (or “suites”)--Required Suite, Standard Suite, Text Suite, etc.  Each suite heading is followed by a list of commands and objects (or “classes”).
  125.  
  126. The dictionary defines every AppleScript word that is recognized by Tex-Edit, but it does not explain all the different possible usages.  Try using Script Editor’s “record” button to see examples of acceptable syntax.
  127.  
  128. What’s an object?
  129.  
  130. In the program:
  131.  
  132.    close window 1 of application "Tex-Edit Plus"
  133.  
  134. the verb “close” is the command and “window 1 of application Tex-Edit Plus” is the object.
  135.  
  136. Many of the objects have “properties.” You can use the Get and Set commands (described below) to extract an object’s property and change it to something different.  Notice how the “of” keyword can chain together a list of objects, allowing you to unambiguously specify any given object.
  137.  
  138.    tell application "Tex-Edit Plus"
  139.       get name of window 1
  140.       set size of character 1 of word 1 of window "untitled" to 24
  141.    end tell
  142.  
  143. In the first command, “name” is a property of the object “window 1 of application Tex-Edit Plus.”  In the second command, “size” is a property of the object “character 1 of word 1 of window "untitled" of application Tex-Edit Plus.”
  144.  
  145. _____________________________
  146.  
  147.  
  148. Commonly-used commands:
  149.  
  150. Here are some of the basic commands understood by most applications along with some simple examples of usage.  Notice that many commands correspond to standard menu items.  Examine each example to see the basic syntax.  Of course, the best way to understand the commands is to actually type them in and try them out.
  151.  
  152. activate
  153.    Brings an application to the front.
  154.    ex:
  155.       activate application "Tex-Edit Plus"
  156.  
  157. quit
  158.    Quits an application.
  159.    ex:
  160.       quit application "Tex-Edit Plus"
  161.       quit application "Tex-Edit Plus" saving no
  162.  
  163. open
  164.    Opens a document file.
  165.    ex:
  166.       tell application "Tex-Edit Plus"
  167.          open file "MacHD:Saving the World"
  168.       end tell
  169.  
  170. print
  171.    Prints a document or window.
  172.    ex:
  173.       print every window of application "Tex-Edit Plus"
  174.       print the last window of application "Tex-Edit Plus"
  175.  
  176. close
  177.    Closes the given window.
  178.    ex:
  179.       close every window of application "Tex-Edit Plus"
  180.       close windows 2 through 4 of application "Tex-Edit Plus"
  181.  
  182. count
  183.    Returns the number of items.
  184.    ex:
  185.       count the words of window 2 of application "Tex-Edit Plus"
  186.       count the windows of application "Tex-Edit Plus"
  187.  
  188. delete
  189.    Deletes the given text.
  190.    ex:
  191.       tell window 1 of application "Tex-Edit Plus"
  192.          delete the first word
  193.          delete every character whose color is red
  194.       end tell
  195.  
  196. get
  197.    Gets whatever information you ask for.  Get and Set are used a lot!
  198.    ex:
  199.       tell application "Tex-Edit Plus"
  200.          get the font of the third word of window 1
  201.          get the position of the first window
  202.       end tell
  203.  
  204. set
  205.    Changes the value of an object’s property.
  206.    ex:
  207.       tell window 1 of application "Tex-Edit Plus"
  208.          set the color of the last word to red
  209.          set the name to "Fun With AppleScript"
  210.       end tell
  211.  
  212. make
  213.    Creates a new window or text item.
  214.    ex:
  215.       tell application "Tex-Edit Plus"
  216.          make new window behind window 1
  217.          make new line at end of text of window 1 with data "Dear Occupant:"
  218.       end tell
  219.  
  220. save
  221.    Saves the window to disk.
  222.    ex:
  223.       tell application "Tex-Edit Plus"
  224.          save the first window in "MacHD:Letter to MicroFlaccid"
  225.       end tell
  226.  
  227. select
  228.    Selects the desired text or window.
  229.    ex:
  230.       tell application "Tex-Edit Plus"
  231.          select the last document of application "Tex-Edit Plus"
  232.          select text from character 1 to character 60000 of window 2
  233.       end tell
  234.  
  235. undo/copy/cut/paste/revert
  236.    Executes the familiar menu command.
  237.    ex:
  238.       tell application "Tex-Edit Plus"
  239.          copy
  240.          copy unstyled
  241.       end tell
  242.  
  243.  
  244. Other commands (specific to Tex-Edit):
  245.  
  246. add line endings
  247.    Adds CRs and LFs to the text.
  248.    ex:
  249.       tell selection of application "Tex-Edit Plus"
  250.          add line endings with crlf
  251.       end tell
  252.  
  253. block format
  254.    Converts the given text into an indented block.
  255.    ex:
  256.       tell the first window of application "Tex-Edit Plus"
  257.          block format the first paragraph with line header ">  "
  258.          block format the last paragraph with line length 45
  259.       end tell
  260.  
  261. change case
  262.    Changes the case of the given text.
  263.    ex:
  264.       tell the middle window of application "Tex-Edit Plus"
  265.          change case of line 3 to uppercase
  266.       end tell
  267.  
  268. search
  269.    Searches for the given text.
  270.    ex:
  271.       search the last window of application "Tex-Edit Plus" for "important item"
  272.  
  273. replace
  274.    Replaces all occurrences of one string with another.
  275.    ex:
  276.       tell application "Tex-Edit Plus"
  277.          replace window 1 from "Copland" to "Allegro"
  278.          replace the last window from multiple spaces to tab
  279.       end tell
  280.  
  281. smarten/stupefy
  282.    Transforms to/from typographical characters.
  283.    ex:
  284.       smarten the first window of application "Tex-Edit Plus" for quotes
  285.  
  286. speak
  287.    Read the given text aloud.
  288.    ex:
  289.       speak "hello"
  290.       speak first paragraph of the third window of application "Tex-Edit Plus"
  291.  
  292. strip
  293.    Remove the specified characters from the text.
  294.    ex:
  295.       tell application "Tex-Edit Plus"
  296.          strip the first window for leading spaces
  297.       end tell
  298.  
  299. strip line endings
  300.    Remove commonly used line ending characters.
  301.    ex:
  302.       tell window 2 of application "Tex-Edit Plus"
  303.          strip line endings with cr
  304.          strip line endings of selection with crlf
  305.       end tell
  306.  
  307.  
  308. Objects:
  309.  
  310. Many objects can be combined with the preceding commands into meaningful statements, however some combinations are not allowed.
  311.  
  312. For example you can:
  313.    close window 1 of application "Tex-Edit Plus"
  314.    print window 1 of application "Tex-Edit Plus"
  315.  
  316. but you can’t:
  317.    delete window 1 of application "Tex-Edit Plus"
  318.    quit window 1 of application "Tex-Edit Plus"
  319.  
  320. “Trial and error” is a good way to get started.  It is also helpful to use Script Editor’s “record” function to watch what happens as you use Tex-Edit.  Note the use of the Get and Set commands to access the properties of each object.  Here are a few of the available objects along with a few of their properties:
  321.  
  322. application
  323.    This is Tex-Edit itself.  Its properties are “global.”
  324.  
  325.    sample properties:
  326.       selection:  The current text selection in the front window.
  327.       search string:  The string used in the Find dialog.
  328.       speech on:  Used to turn on/off the Speech Manager functions.
  329.  
  330.    ex:
  331.       tell application "Tex-Edit Plus"
  332.          set font of selection to "Monaco"
  333.          set search string to "interesting stuff"
  334.          set speech on to true
  335.          get selection
  336.       end tell
  337.  
  338. window
  339.    This is synonymous with “document.”
  340.  
  341.    sample properties:
  342.       name:  The title of the document.
  343.       position:  The coordinates of the top left corner.
  344.       justification:  Used to set text justification in the window.
  345.  
  346.    ex:
  347.       tell application "Tex-Edit Plus"
  348.          set the position of some window to {125, 250}
  349.          set justification of every document to center
  350.          get name of window 1
  351.       end tell
  352.  
  353. character
  354.    This is a single character in a window.
  355.  
  356.    sample properties:
  357.       font:  The font face of the character.
  358.       size:  Font size of the character.
  359.       style:  Styles of the character (bold, italic, etc.).
  360.       color:  Color of the character.
  361.  
  362.    ex:
  363.       tell window 3 of application "Tex-Edit Plus"
  364.          set size of (text from character 1 to word 10) to 24
  365.          set the style of character 1 to {bold, italic, underline}
  366.          set color of the last character to red
  367.          get the font of character 1
  368.       end tell
  369.  
  370. word-line-paragraph-text
  371.    These objects share all the properties of character.
  372.  
  373.    ex:
  374.       tell window 1 of application "Tex-Edit Plus"
  375.          set the color of paragraph 2 to blue
  376.          set size of line 1 to 24
  377.          set style of (text from word 2 to word 4) to bold
  378.          get the font of word 1
  379.       end tell
  380.  
  381. _____________________________
  382.  
  383.  
  384. AppleScript seems so simple.  Are there any limits?
  385.  
  386. This document barely brushes the surface of the possibilities.  My favorite trick is to use voice-actuated “style sheet” scripts to switch styles as I type.
  387.  
  388. The sample scripts in Tex-Edit’s “Scripts” folder will help you get started.  Feel free to examine them and change them as much as you wish.
  389.  
  390. Where can I find more sample scripts?
  391.  
  392. Doug Adams maintains an archive of hints, tips and AppleScript examples at:
  393. <http://www.malcolmadams.com/te/>
  394.  
  395. In addition, there are hundreds of useful scripts located all over the web, available to anyone with a modem and a phone jack.  Be sure to check out the numerous AppleScript extensions (OSAX) that add even more power.
  396.  
  397. So, what are you waiting for?  You have nothing to lose but your tired fingers.
  398.  
  399. (Document Revised 8/4/98)